package com.ztgf.shopping.api.cms.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.shopping.models.bo.shop.ShopCollectBO;
import com.ztgf.shopping.models.bo.shop.ShopCollectSaveBO;
import com.ztgf.shopping.models.dto.shop.ShopCollectDTO;
import com.ztgf.shopping.models.service.shop.IShopCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 用户收藏表后台接口类
 *
 * @author zhanghualang
 * @Date 2020-05-26
 */

@RestController
@RequestMapping("/cms/shopCollect")
@Slf4j
@Api(value = "ShopCollectController",tags = "用户收藏表接口")
public class ShopCollectController {
    @DubboReference
    private IShopCollectService IShopCollectService;

    @PostMapping("/save")
    @ApiOperation(value = "保存用户收藏表",notes = "保存用户收藏表")
    public Result save( ShopCollectSaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "添加用户收藏表",notes = "添加用户收藏表")
    @PostMapping(value = "/insert")
    public Result insert( ShopCollectSaveBO insertSaveBO) {
        boolean flag = IShopCollectService.add(insertSaveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "修改用户收藏表",notes = "修改用户收藏表")
    @PostMapping("/update")
    public Result update( ShopCollectSaveBO updateSaveBO){
        ShopCollectDTO shopCollectDTO = new ShopCollectDTO();
        BeanUtils.copyProperties(updateSaveBO, shopCollectDTO);
        shopCollectDTO.setId(updateSaveBO.getId());
        boolean flag = IShopCollectService.updateById(shopCollectDTO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @ApiOperation(value = "删除用户收藏表",notes = "删除用户收藏表")
    @ApiImplicitParam(name = "id",value = "用户收藏表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = IShopCollectService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除用户收藏表",notes = "批量删除用户收藏表")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        IShopCollectService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找用户收藏表",notes = "根据id查找用户收藏表")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopCollectDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        ShopCollectDTO shopCollectDTO = IShopCollectService.selectById(id);
        return new Result<>(shopCollectDTO);
    }

    @ApiOperation(value = "用户收藏表列表",notes = "用户收藏表列表")
    @PostMapping("/queryPageList")
    public Result<Page<ShopCollectDTO>> queryPageList( ShopCollectBO shopCollectBO){
        Wrapper<ShopCollectDTO> wrapper = new EntityWrapper<>();

        if(shopCollectBO.getCollectShopId() != null){
            wrapper.eq("collect_shop_id", shopCollectBO.getCollectShopId());
        }

        if(shopCollectBO.getStatus() != null){
            wrapper.eq("status", shopCollectBO.getStatus());
        }

        if(shopCollectBO.getType() != null){
            wrapper.eq("type", shopCollectBO.getType());
        }

        wrapper.orderBy("create_time", false);//降序

        Page<ShopCollectDTO> page = new Page<>(shopCollectBO.getPageNo(), shopCollectBO.getPageSize());
        page = IShopCollectService.selectPage(page, wrapper);
        return new Result<>(page);
    }
}
